package com.bcbim.api_web.space_category.controller;

import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.common.util.ParamCheck;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceUnassignedDTO;
import com.bcbim.operation_maintenance_interface.space.service.SpaceService;
import com.bcbim.operation_maintenance_interface.space_category.dao.SpaceCategoryDAO;
import com.bcbim.operation_maintenance_interface.space_category.service.SpaceCategoryService;
import com.bcbim.operation_maintenance_interface.space_category.vo.ListSpaceCategoryVO;
import com.bcbim.operation_maintenance_interface.space_category.vo.SpaceCategoryTreeVO;
import com.bcbim.operation_maintenance_interface.space_category.vo.SpaceCategoryVO;
import com.bcbim.operation_maintenance_interface.space_type.dao.SpaceTypeDAO;
import com.bcbim.operation_maintenance_interface.space_type.service.SpaceTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version V 1.0.0
 * @Description：空间类别controller
 * @Author: haomeijie
 * @Date: 2022/3/5 9:13
 **/
@RestController
@RequestMapping("/spaceCategory")
public class SpaceCategoryController {

    @Reference(protocol = "dubbo", group = "spaceCategory", version = "1.0.0")
    private SpaceCategoryService spaceCategoryServiceImpl;

    @Reference(protocol = "dubbo", group = "spaceType", version = "1.0.0")
    private SpaceTypeService spaceTypeServiceImpl;

    @Reference(protocol = "dubbo", group = "space", version = "1.0.0")
    private SpaceService spaceServiceImpl;

    /**
     * 新增空间类别
     *
     * @param spaceCategoryDAO
     * @return ReturnMsg
     * @Author: haomeijie
     * @Date: 2022/3/5 9:34
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ReturnMsg insert(SpaceCategoryDAO spaceCategoryDAO) {
        //参数校验
        try {
            //类别名称限20；顶级类别码值；是否可占用0是1否；是否可计算0是1否
            ParamCheck.stringNotEmpty(spaceCategoryDAO.getSpaceCategory(), 20);
            ParamCheck.stringNotEmpty(spaceCategoryDAO.getTopCategory(), 1);
            ParamCheck.stringNotEmpty(spaceCategoryDAO.getOccupy(), 1);
            ParamCheck.stringNotEmpty(spaceCategoryDAO.getCalculate(), 1);
            ParamCheck.idCheck(spaceCategoryDAO.getProjectId());
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //类别名称不能重复
        if (spaceCategoryServiceImpl.checkSpaceCategory(spaceCategoryDAO.getSpaceCategory(), spaceCategoryDAO.getProjectId(), null) > 0) {
            return ReturnMsg.consFail("空间类别重复");
        }
        //整理参数
        spaceCategoryDAO.setId(GuidUtil.get())
                .setCreateBy(AuthUtil.getUserIdDecrypt())
                .setCreateTime(LocalDateTime.now())
                .setDel(Constant.NOT_DEL);
        //调用服务
        int i = spaceCategoryServiceImpl.insertSpaceCategory(spaceCategoryDAO);
        return i == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * 查看空间类别详情
     *
     * @param id
     * @return ReturnMsg
     * @Author: haomeijie
     * @Date: 2022/3/5 9:54
     */
    @RequestMapping("/getDetail")
    public ReturnMsg getDetail(String id) {
        //参数校验
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //调用服务
        SpaceCategoryVO spaceCategoryVO = spaceCategoryServiceImpl.getSpaceCategory(id);
        return ReturnMsg.consSuccess(spaceCategoryVO);
    }

    /**
     * 编辑空间类别
     *
     * @param spaceCategoryDAO
     * @return ReturnMsg
     * @Author: haomeijie
     * @Date: 2022/3/5 9:44
     */
    @RequestMapping("/update")
    public ReturnMsg update(SpaceCategoryDAO spaceCategoryDAO) {
        //参数校验
        try {
            //类别名称限20；顶级类别码值；是否可占用0是1否；是否可计算0是1否
            ParamCheck.stringNotEmpty(spaceCategoryDAO.getSpaceCategory(), 20);
            ParamCheck.stringNotEmpty(spaceCategoryDAO.getTopCategory(), 1);
            ParamCheck.stringNotEmpty(spaceCategoryDAO.getOccupy(), 1);
            ParamCheck.stringNotEmpty(spaceCategoryDAO.getCalculate(), 1);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //类别名称不能重复
        if (spaceCategoryServiceImpl.checkSpaceCategory(spaceCategoryDAO.getSpaceCategory(), spaceCategoryDAO.getProjectId(), spaceCategoryDAO.getId()) > 0) {
            return ReturnMsg.consFail("空间类别重复");
        }
        //整理参数
        spaceCategoryDAO.setUpdateBy(AuthUtil.getUserIdDecrypt())
                .setUpdateTime(LocalDateTime.now());
        //调用服务
        int i = spaceCategoryServiceImpl.updateSpaceCategory(spaceCategoryDAO);
        //如果从可占用变为不可占用，该类别/类型下所有空间中的人员‘办公地点’属性被清除
//        if (spaceCategoryDAO.getOccupy().equals(Constant.NOT_OCCUPY)) {
//            //修改人员办公地点
//            staffServiceImpl.updateStaffByCategoryId(spaceCategoryDAO.getId());
//        }
        return i == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }


    /**
     * 删除空间类别
     *
     * @param id
     * @return ReturnMsg
     * @Author: haomeijie
     * @Date: 2022/3/5 10:59
     */
    @RequestMapping("/delete")
    public ReturnMsg delete(String id, String projectId) {
        //参数校验
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //删除空间类别下的空间类型
        List<String> list = spaceTypeServiceImpl.getSpaceTypeId(id);
        if (CollectionUtils.isNotEmpty(list)) {
            for (String string : list) {
                SpaceTypeDAO spaceTypeDAO = new SpaceTypeDAO();
                spaceTypeDAO.setId(string);
                spaceTypeDAO.setDel(Constant.DEL);
                spaceTypeDAO.setUpdateTime(LocalDateTime.now());
                spaceTypeDAO.setUpdateBy(AuthUtil.getUserIdDecrypt());
                spaceTypeServiceImpl.updateSpaceType(spaceTypeDAO);
            }
        }
        //整理参数
        SpaceCategoryDAO spaceCategoryDAO = new SpaceCategoryDAO();
        spaceCategoryDAO.setId(id)
                .setUpdateBy(AuthUtil.getUserIdDecrypt())
                .setUpdateTime(LocalDateTime.now())
                .setDel(Constant.DEL);
        //调用服务
        int i = spaceCategoryServiceImpl.updateSpaceCategory(spaceCategoryDAO);

        //修改人员办公地点
//        staffServiceImpl.updateStaffByCategoryId(id);

        //清空指定空间的类别
        //获取未指派id
        String unassignedId = spaceCategoryServiceImpl.getUnassignedId(Constant.UNASSIGNED, projectId);
        //整理参数
        SpaceUnassignedDTO spaceUnassignedDTO = new SpaceUnassignedDTO();
        spaceUnassignedDTO.setUnassignedId(unassignedId)
                .setCategoryId(id);
        spaceServiceImpl.updateSpaceType(spaceUnassignedDTO);
        return i == 1 ? ReturnMsg.consSuccess(Constant.DEL_SUCCESS) : ReturnMsg.consFail(Constant.DEL_FAIL);
    }

    /**
     * 类别-类型树形结构
     *
     * @Author: haomeijie
     * @Date: 2022/3/8 17:20
     */
    @RequestMapping("/listSpaceCategoryTree")
    public ReturnMsg listSpaceCategoryTree(String projectId) {
        List<SpaceCategoryTreeVO> list = spaceCategoryServiceImpl.listSpaceCategoryTree(projectId);
//        List<ListSpaceCategoryTreeVO> treeList = spaceCategoryServiceImpl.listSpaceByCategory(new SpaceDTO());
//        List<SpaceCategoryTreeVO> list = new ArrayList<>();
//        List<SpaceCategoryTreeVO> childList;
//
//        //整理数据
//        for (ListSpaceCategoryTreeVO listSpaceCategoryTreeVO : treeList) {
//            //类别
//            SpaceCategoryTreeVO categoryTreeVO = new SpaceCategoryTreeVO();
//            categoryTreeVO.setId(listSpaceCategoryTreeVO.getSpaceCategoryId());
//            categoryTreeVO.setParentId(null);
//            categoryTreeVO.setName(listSpaceCategoryTreeVO.getSpaceCategory());
//            childList = new ArrayList<>();
//            if (ObjectUtils.isNotEmpty(listSpaceCategoryTreeVO.getChildList())) {
//                //类型
//                for (ListSpaceCategoryTreeVO treeVO : listSpaceCategoryTreeVO.getChildList()) {
//                    SpaceCategoryTreeVO typeTreeVO = new SpaceCategoryTreeVO();
//                    typeTreeVO.setId(treeVO.getSpaceTypeId());
//                    typeTreeVO.setName(treeVO.getSpaceType());
//                    typeTreeVO.setParentId(treeVO.getSpaceCategoryId());
//                    typeTreeVO.setChild(null);
//                    childList.add(typeTreeVO);
//                }
//            }
//            categoryTreeVO.setChild(childList);
//            list.add(categoryTreeVO);
//        }
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 类别-类型列表
     *
     * @param projectId
     * @Author: haomeijie
     * @Date: 2022/3/22 18:31
     */
    @RequestMapping("/listSpaceCategory")
    public ReturnMsg listSpaceCategory(String projectId) {
        String name = "未指派";
        List<ListSpaceCategoryVO> list = spaceCategoryServiceImpl.listSpaceCategory(name, projectId);
        return ReturnMsg.consSuccess(list);

    }


    /**
     * 判断指派过该类别的空间是否有人员
     *
     * @param id
     * @return Boolean
     * @Author: haomeijie
     * @Date: 2022/4/29 11:02
     */
    @RequestMapping("/checkExitStaff")
    public ReturnMsg checkExitStaff(String id) {
        //参数校验
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        Boolean b = spaceCategoryServiceImpl.checkCategoryExitStaff(id);
        return ReturnMsg.consSuccess(b);
    }

}
